cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(redis VERSION 6.0)
#set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ./bin)
message(CMAKE_RUNTIME_OUTPUT_DIRECTORY is:${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
#if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type defined; defaulting to 'Debug'")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
        "The type of build. Possible values are: Debug, Release,
RelWithDebInfo and MinSizeRel.")

#endif()
message(STATUS "Host is: ${CMAKE_HOST_SYSTEM}. Build target is:
${CMAKE_SYSTEM}")
get_filename_component(REDIS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
message(STATUS "Project root directory is: ${REDIS_ROOT}")
# Just for debugging when handling a new platform.
if (false)
    message("C++ compiler supports these language features:")
    foreach (i ${CMAKE_CXX_COMPILE_FEATURES})
        message(" ${i}")
    endforeach ()
endif ()
message(STATUS "Generating release.h...")
execute_process(
        COMMAND sh -c ./mkreleasehdr.sh
        WORKING_DIRECTORY ${REDIS_ROOT}/src/
)
file(COPY ${REDIS_ROOT}/src/version.h DESTINATION ${REDIS_ROOT}/release/ FOLLOW_SYMLINK_CHAIN)
file(RENAME ${REDIS_ROOT}/src/release.h ${REDIS_ROOT}/release/release.h)
# file(REMOVE ${REDIS_ROOT}/src/release.h)

add_subdirectory(deps)
add_subdirectory(src/modules)

include_directories(${CMAKE_SOURCE_DIR}/deps/hiredis)
include_directories(${CMAKE_SOURCE_DIR}/deps/lua/src)
include_directories(${CMAKE_SOURCE_DIR}/deps/jemalloc/include)
include_directories(${CMAKE_SOURCE_DIR}/deps/fpconv)
include_directories(${CMAKE_SOURCE_DIR}/deps/hdr_histogram)
include_directories(${REDIS_ROOT}/release/)

link_directories(${CMAKE_SOURCE_DIR}/deps/hiredis)
link_directories(${CMAKE_SOURCE_DIR}/deps/lua/src)
link_directories(${CMAKE_SOURCE_DIR}/deps/jemalloc/)
link_directories(${CMAKE_SOURCE_DIR}/deps/fpconv)
link_directories(${CMAKE_SOURCE_DIR}/deps/hdr_histogram)
link_directories(deps/hdr_histogram)
link_directories(deps/fpconv)
link_directories(deps/jemalloc)
link_directories(deps/lua)
link_directories(deps/hiredis)



set(SRC_SERVER_FS
src/adlist.c 
src/quicklist.c 
src/ae.c 
src/anet.c 
src/dict.c 
# src/server.c 
src/sds.c 
src/zmalloc.c 
src/lzf_c.c 
src/lzf_d.c 
src/pqsort.c 
src/zipmap.c 
src/sha1.c 
src/ziplist.c 
src/release.c 
src/networking.c 
src/util.c 
src/object.c 
src/db.c 
src/replication.c 
src/rdb.c 
src/t_string.c 
src/t_list.c 
src/t_set.c 
src/t_zset.c 
src/t_hash.c 
src/config.c 
src/aof.c 
src/pubsub.c 
src/multi.c 
src/debug.c 
src/sort.c 
src/intset.c 
src/syncio.c 
src/cluster.c 
src/crc16.c 
src/endianconv.c 
src/slowlog.c 
src/eval.c 
src/bio.c 
src/rio.c 
src/rand.c 
src/memtest.c 
src/syscheck.c 
src/crcspeed.c 
src/crc64.c 
src/bitops.c 
src/sentinel.c 
src/notify.c 
src/setproctitle.c 
src/blocked.c 
src/hyperloglog.c 
src/latency.c 
src/sparkline.c 
src/redis-check-rdb.c 
src/redis-check-aof.c 
src/geo.c 
src/lazyfree.c 
src/module.c 
src/evict.c 
src/expire.c 
src/geohash.c 
src/geohash_helper.c 
src/childinfo.c 
src/defrag.c 
src/siphash.c 
src/rax.c 
src/t_stream.c 
src/listpack.c 
src/localtime.c 
src/lolwut.c 
src/lolwut5.c 
src/lolwut6.c 
src/acl.c 
src/tracking.c 
src/socket.c 
src/tls.c 
src/sha256.c 
src/timeout.c 
src/setcpuaffinity.c 
src/monotonic.c 
src/mt19937-64.c 
src/resp_parser.c 
src/call_reply.c 
src/script_lua.c 
src/script.c 
src/functions.c 
src/function_lua.c 
src/commands.c 
src/strl.c 
src/connection.c 
src/unix.c 
src/logreqres.c
src/server.h
src/cli_common
)        


set(SRC_SERVER src/server.c ${SRC_SERVER_FS})
set(SRC_CLI
        # src/anet.c
        # src/sds.c
        # src/adlist.c
        # src/redis-cli.c
        # src/zmalloc.c
        # src/release.c
        # src/ae.c
        # src/crc64.c
        # src/crc16.c
        # src/dict.c
        # src/siphash.c
        # src/server.h
        # src/debug.c
        # src/tls.c 
        # src/sha256.c 

        src/anet.c 
        src/adlist.c 
        src/dict.c 
        src/redis-cli.c 
        src/zmalloc.c 
        src/release.c 
        src/ae.c 
        src/redisassert.c 
        src/crcspeed.c 
        src/crc64.c 
        src/siphash.c 
        src/crc16.c 
        src/monotonic.c 
        src/cli_common.c 
        src/mt19937-64.c 
        src/strl.c 
        src/cli_commands.c

        )
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    # better not to work with jemalloc
endif()
set(EXECUTABLE_OUTPUT_PATH src)

add_library(rediss src/server.c ${SRC_SERVER_FS})
set_property(TARGET rediss PROPERTY C_STANDARD 99)
set_property(TARGET rediss PROPERTY CXX_STANDARD 11)
set_property(TARGET rediss PROPERTY CXX_STANDARD_REQUIRED ON)
target_include_directories(rediss
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/lua/src
        )
target_link_libraries(rediss
        PRIVATE pthread
        PRIVATE m
        PRIVATE lua
        PRIVATE linenoise
        PRIVATE hiredis
        PRIVATE fpconv
        PRIVATE hdrhistogram
        )



# add_executable(rediss2 src/server.c)
# set_property(TARGET rediss2 PROPERTY C_STANDARD 99)
# set_property(TARGET rediss2 PROPERTY CXX_STANDARD 11)
# set_property(TARGET rediss2 PROPERTY CXX_STANDARD_REQUIRED ON)
# target_include_directories(rediss2
#         PRIVATE ${REDIS_ROOT}/deps/hiredis
#         PRIVATE ${REDIS_ROOT}/deps/linenoise
#         PRIVATE ${REDIS_ROOT}/deps/lua/src
#         )
# target_link_libraries(rediss2
#         PRIVATE pthread
#         PRIVATE m
#         PRIVATE lua
#         PRIVATE linenoise
#         PRIVATE hiredis
#         PRIVATE fpconv
#         PRIVATE hdrhistogram
#         PRIVATE rediss
#         )


add_executable(redis-server ${SRC_SERVER})

set_property(TARGET redis-server PROPERTY C_STANDARD 99)
set_property(TARGET redis-server PROPERTY CXX_STANDARD 11)
set_property(TARGET redis-server PROPERTY CXX_STANDARD_REQUIRED ON)


target_include_directories(redis-server
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/lua/src
        )

target_link_libraries(redis-server
        PRIVATE pthread
        PRIVATE m
        PRIVATE lua
        PRIVATE linenoise
        PRIVATE hiredis
        PRIVATE fpconv
        PRIVATE hdrhistogram
        )


add_executable(redis-cli ${SRC_CLI})

set_property(TARGET redis-cli PROPERTY C_STANDARD 99)
set_property(TARGET redis-cli PROPERTY CXX_STANDARD 11)
set_property(TARGET redis-cli PROPERTY CXX_STANDARD_REQUIRED ON)

target_include_directories(redis-cli
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/lua/src
        )

target_link_libraries(redis-cli
        PRIVATE pthread
        PRIVATE m
        PRIVATE linenoise
        PRIVATE hiredis
        )


link_directories(deps/hiredis/ deps/linenoise/ diredeps/lua/src)



if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
        message(STATUS "current system: Linux")
        target_link_libraries(redis-server
                PRIVATE dl
        )
        set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -fPIC )
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
        message(STATUS "current system: Mac")
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
        message(STATUS "current system: Windows")
else()
        message(STATUS "current system: ${CMAKE_SYSTEM_NAME}")
endif()

install(TARGETS redis-server
        RUNTIME DESTINATION bin
)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -c")
